<script setup lang="ts" name="LoadToken">
import { onMounted } from "vue";
import { useToken } from "@/store/token";
import { useRoute } from "vue-router";
import { showFailToast } from "vant";
import router from "@/router";
import { GetAreaId } from "@/api/common";
import { useDictInfo } from "@/store/dict";
onMounted(() => {
  const route = useRoute();
  const token = route.query.token;
  const menu = route.query.menu;
  if (!token) {
    showFailToast("没有Token");
    return;
  }
  if (!menu) {
    showFailToast("没有指定地址");
    return;
  }

  localStorage.clear();

  const dictStore = useDictInfo();
  dictStore.clearAllDict();

  const tokenStore = useToken();
  tokenStore.removeToken();
  tokenStore.removeAreaId();

  tokenStore.setToken(token as string);
  localStorage.setItem("token", token as string);

  GetAreaId().then((res: any) => {
    if (!res.data) {
      showFailToast("该账号未绑定区域");
    } else {
      tokenStore.setAreaId(res.data as string);
      localStorage.setItem("areaId", res.data as string);
      dictStore.initAllDict();
      router.push("/" + menu);
    }
  });
});
</script>
